//
//  Connect.m
//  电销宝
//
//  Created by welldone on 13-4-10.
//  Copyright (c) 2013年 com.welldone. All rights reserved.
//

#import "Connect.h"
#import "CheckNetWorkTool.h"
#import "StringTool.h"
#import "JSONKit.h"
#import "MBProgressHUD+MJ.h"

#define  App_version_head_key @"appServerVison"




@implementation ResulteMod
@synthesize resltState,resultData,dataMod;

@end



//无网络，网络错误  errcode！=0     返回nil，
@interface Connect ()

+(NSString *)doGet:(NSString *)urlString;
+(NSString *)doPost:(NSString *)urlString withData:(NSString*)badyString;

+(ResulteMod*)checkError:(NSString*)resString;
@end


@implementation Connect



//发送get请求，缓存返回的数据
+(NSString *)doGet:(NSString *)urlString
{
    
    if ([CheckNetWorkTool isExistenceNetwork]) {
        
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
        
        
        NSLog(@"doGet 访问%@",urlString);
        NSURLCache *urlCache = [NSURLCache sharedURLCache];
        /* 设置缓存的大小为1M*/
        [urlCache setMemoryCapacity:1*1024*1024];
        NSURL *url=[NSURL URLWithString:urlString];
        
        //创建一个请求
        NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:8.0f];
        [urlRequest setHTTPMethod:@"GET"];
        [urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
        NSLog(@"timeout: %f", urlRequest.timeoutInterval);   // Displays 240.000000
        
        //定义
        NSHTTPURLResponse* urlResponse = nil;
        NSError *error = [[NSError alloc] init];
        
        NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&error];
        
        NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        result = [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//去前后空格
        NSLog(@"DoGet 获取数据%@",result);
        
        
        
        NSLog(@"请求数据完毕");
        if ([result length]>0) {
            NSString *key = [urlString MD5Hash];
            [FTWCache setObject:responseData forKey:key];//写入缓存
            NSLog(@"写入缓存完毕");
        }
        
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        
        
        if (error.code!=0) {//网络错误
            return nil;
        }else{
            return result;
        }
    }
    return nil;
}



+(ResulteMod*)doGetWithCaChe:(NSString *)urlString{
    
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *key = [urlString MD5Hash];
    NSData *resData = [FTWCache objectForKey:key];//从缓存中获取数据
    NSString *result=@"";
    
    if ([resData length]>0) {//有数据
        
        result = [[NSString alloc] initWithData:resData encoding:NSUTF8StringEncoding];
        NSLog(@"doGetWithCaChe 访问%@",urlString);
        NSLog(@"doGetWithCaChe 从缓存得到数据%@",result);
        
    } else {
        NSLog(@"缓存没有数据，请求数据");
        result =[self doGet:urlString];
    }
    
    ResulteMod *mod = [self checkError:result];
    
    return mod;
}



+(ResulteMod*)doGetWithOutCaChe:(NSString *)urlString{
    
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    ResulteMod *mod = [ResulteMod new];
    
    if ([CheckNetWorkTool isExistenceNetwork]) {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
        
        NSLog(@"doGet 访问%@",urlString);
        NSURLCache *urlCache = [NSURLCache sharedURLCache];
        /* 设置缓存的大小为1M*/
        [urlCache setMemoryCapacity:1*1024*1024];
        NSURL *url=[NSURL URLWithString:urlString];
        
        //创建一个请求
        NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:8.0f];
        [urlRequest setHTTPMethod:@"GET"];
        [urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
        NSLog(@"timeout: %f", urlRequest.timeoutInterval);   // Displays 240.000000
        
        //定义
        NSHTTPURLResponse* urlResponse = nil;
        NSError *error = [[NSError alloc] init];
        
        NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&error];
        
        NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        result = [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//去前后空格
        
        NSLog(@"DoGet 获取数据%@",result);
        
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        
        
        mod = [self checkError:result];
        
        if (error.code!=0)
        {
            mod.resltState = ResultNetError;
            mod.resultTip = @"没有网络";
            
        }else
        {
            mod.resltState = ResultSuccess;
        }
    }else
    {
        mod.resltState = ResultNetError;
        mod.resultTip = @"没有网络";
        
    }
    
    return mod;
}



+(void)doGetWithCaChe:(NSString *)urlString withCallBack:(FinishCallBack)block
{
    
    block([Connect doGetWithCaChe:urlString]);
}






//发送get  不缓存
+(void)doGetWithOutCaChe:(NSString *)urlString withCallBack:(FinishCallBack)block{
    
    block([Connect doGetWithOutCaChe:urlString]);
}






//发送POST请求
+(NSString *)doPost:(NSString *)urlString withData:(NSString*)badyString
{
    if ([CheckNetWorkTool isExistenceNetwork])
    {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
        
        NSString *urlAll = [urlString stringByAppendingString:badyString];
        NSLog(@"doPost 访问%@",urlAll);
        
        NSURL *url=[NSURL URLWithString:urlString];
        
        NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
        [urlRequest setTimeoutInterval:8.0f];//设置超时秒
        [urlRequest setHTTPMethod:@"POST"];
        [urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
        [urlRequest setHTTPBody:[badyString dataUsingEncoding:NSUTF8StringEncoding]];//???
        
        //定义
        NSHTTPURLResponse* urlResponse = nil;
        NSError *error = [[NSError alloc] init];
        
        NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&error];
        
        NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        result = [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//去前后空格
        
        NSLog(@"DoPost 获取数据%@",result);
        
        NSLog(@"请求数据完毕");
        if ([result length]>0) {
            [FTWCache setObject:responseData forKey:[urlAll MD5Hash]];//写入缓存
            NSLog(@"写入缓存完毕");
        }
        
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        
        if (error.code!=0) {
            return nil;
        }else{
            return result;
        }
        
    }else{
        return nil;
    }
}




+(ResulteMod*)doPostWithCache:(NSString *)urlString withData:(NSString*)badyString{
    
    badyString = [badyString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *fullUrl = [urlString stringByAppendingString:badyString];
    NSString *key = [fullUrl MD5Hash];
    NSData *resData = [FTWCache objectForKey:key];//从缓存中获取数据
    NSString *result = nil;
    
    if ([resData length]>0) {//有数据
        
        result = [[NSString alloc] initWithData:resData encoding:NSUTF8StringEncoding];
        NSString *key = [urlString stringByAppendingString:badyString];
        NSLog(@"doPos 访问%@",key);
        NSLog(@"doPostWithCache 从缓存得到数据%@",result);
        
    } else {
        NSLog(@"缓存没有数据，请求数据");
        
        result =[self doPost:urlString withData:badyString];
    }
    
    ResulteMod *mod = [self checkError:result];
    
    return mod;
}

+(ResulteMod *)doPostCaCheWithUrl:(NSString *)urlString withParamDic:(NSDictionary *)dict
{
    
    NSData* jsonData =[NSJSONSerialization dataWithJSONObject:dict
                                                      options:NSJSONWritingPrettyPrinted error:nil];
    NSString *badyString=[[NSString alloc] initWithData:jsonData
                                               encoding:NSUTF8StringEncoding];
    badyString = [badyString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *fullUrl = [urlString stringByAppendingString:badyString];
    NSString *key = [fullUrl MD5Hash];
    NSData *resData = [FTWCache objectForKey:key];//从缓存中获取数据
    NSString *result = nil;
    
    if ([resData length]>0) {//有数据
        
        result = [[NSString alloc] initWithData:resData encoding:NSUTF8StringEncoding];
        NSString *key = [urlString stringByAppendingString:badyString];
        NSLog(@"doPos 访问%@",key);
        NSLog(@"doPostWithCache 从缓存得到数据%@",result);
        
    } else {
        NSLog(@"缓存没有数据，请求数据");
        
        result = [self doPost:urlString withDic:dict];
        
    }
    ResulteMod *mod = [self checkError:result];
    return mod;
}



//发送POST请求,body参数为dic(把请求到内容写入缓存）
+(NSString *)doPost:(NSString *)urlString withDic:(NSDictionary *)dict
{
    if ([CheckNetWorkTool isExistenceNetwork])
    {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
        
        
        NSData* jsonData =[NSJSONSerialization dataWithJSONObject:dict
                                                          options:NSJSONWritingPrettyPrinted error:nil];
        NSString *badyString=[[NSString alloc] initWithData:jsonData
                                                   encoding:NSUTF8StringEncoding];
        
        
        badyString = [badyString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *urlAll = [urlString stringByAppendingString:badyString];
        NSLog(@"doPost 访问%@",urlAll);
        
        NSURL *url=[NSURL URLWithString:urlString];
        
        NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
        [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [urlRequest setValue:@"2" forHTTPHeaderField:App_version_head_key];
        [urlRequest setTimeoutInterval:8.0f];//设置超时秒
        [urlRequest setHTTPMethod:@"POST"];
        [urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
        
        
        NSData  *jsData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
        
        
        [urlRequest setHTTPBody:jsData];//???
        
        //定义
        NSHTTPURLResponse* urlResponse = nil;
        NSError *error = [[NSError alloc] init];
        
        NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&error];
        
        NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        result = [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//去前后空格
        
        NSLog(@"DoPost 获取数据%@",result);
        
        NSLog(@"请求数据完毕");
        if ([result length]>0) {
            [FTWCache setObject:responseData forKey:[urlAll MD5Hash]];//写入缓存
            NSLog(@"写入缓存完毕");
        }
        
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        
        if (error.code!=0) {
            return nil;
        }else{
            return result;
        }
        
    }else{
        return nil;
    }
}

+(ResulteMod *)doPostOutCaCheWithUrl:(NSString *)urlString withParamDic:(NSDictionary *)dict
{
    
    ResulteMod *mod = [ResulteMod new];
  
    if ([CheckNetWorkTool isExistenceNetwork])
    {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
        
        
        NSURL *url=[NSURL URLWithString:urlString];
        
        NSLog(@"======withParamDic %@",dict);
        
        NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
        [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [urlRequest setValue:@"2" forHTTPHeaderField:App_version_head_key];
        [urlRequest setTimeoutInterval:8.0f];//设置超时秒
        [urlRequest setHTTPMethod:@"POST"];
        [urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
        
        
        NSData  *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
        
        
        [urlRequest setHTTPBody:jsonData];//???
        
        NSLog(@"访问的url %@", url);   // Displays 240.000000
        
        //定义
        NSHTTPURLResponse* urlResponse = nil;
        NSError *error = [[NSError alloc] init];
        
        NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&error];
        
        NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        result = [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//去前后空格
        
        NSLog(@"DoPost====== 获取数据%@",result);
        
        NSLog(@"请求数据完毕");
        
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        mod = [self checkError:result];
        
        
        if (error.code!=0) {
            mod.resltState = ResultNetError;
        }else{
            mod.resltState = ResultSuccess;
        }
        
        return mod;
    }else{
        
        mod.resltState = ResultNetError;
        mod.resultTip = @"没有网络";
        return mod;
    }
//    return mod;
    
}



+(void)doPostWithCaCheWithUrl:(NSString *)urlString withFormdata:(NSDictionary *)formdata withCallBack:(FinishCallBack)callBack{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        ResulteMod *mod = [ResulteMod new];
        if ([CheckNetWorkTool isExistenceNetwork])
        {
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
            
            
            NSString *fullUrl = [urlString stringByAppendingString:formdata.JSONString];
            NSString *key = [fullUrl MD5Hash];
            NSData *resData = [FTWCache objectForKey:key];//从缓存中获取数据
            
            NSError *error;
            NSString *result = nil;
            
            
            if ([resData length]>0) {//有数据
                
                result = [[NSString alloc] initWithData:resData encoding:NSUTF8StringEncoding];
                NSLog(@"doPos 访问%@",fullUrl);
                NSLog(@"doPostWithCache 从缓存得到数据%@",result);
                
            } else {
                
                
                NSLog(@"缓存没有数据，请求数据");
                
                
                NSURL *url=[NSURL URLWithString:urlString];
                
                NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
                
                NSString *boundary = @"12436041281943726692693274280";
                
                
                //设置请求体中内容
                NSMutableString *bodyString = [[NSMutableString alloc]init];
                int count = (int)([[formdata allKeys] count]-1);
                for (int i=count; i>=0; i--) {
                    
                    NSString *key = [formdata allKeys][i];
                    NSString *value = [formdata allValues][i];
                    
                    [bodyString appendFormat:@"-----------------------------%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n",boundary,key,value];
                }
                
                [bodyString appendFormat:@"-----------------------------%@--\r\n", boundary];
                NSMutableData *bodyData = [[NSMutableData alloc]initWithLength:0];
                NSData *bodyStringData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
                [bodyData appendData:bodyStringData];
                
                NSString *contentLength = [NSString stringWithFormat:@"%ld",(long)[bodyData length]];
                
                NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=---------------------------%@", boundary];
                
                [request setValue:contentType forHTTPHeaderField:@"Content-Type"];
                [request setValue:@"2" forHTTPHeaderField:App_version_head_key];
                [request setValue:contentLength forHTTPHeaderField:@"Content-Length"];
                [request setHTTPBody:bodyData];
                [request setHTTPMethod:@"POST"];
                
                
                
                NSLog(@"timeout: %f", request.timeoutInterval);   // Displays 240.000000
                
                //定义
                NSHTTPURLResponse* urlResponse = nil;
                error = [[NSError alloc] init];
                
                NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
                
                result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
                result = [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//去前后空格
                
                NSLog(@"请求数据完毕");
                if ([result length]>0) {
                    [FTWCache setObject:responseData forKey:[fullUrl MD5Hash]];//写入缓存
                    NSLog(@"写入缓存完毕");
                }
                
                NSLog(@"DoPost====== 获取数据%@",result);
                
                NSLog(@"请求数据完毕");
            }
            
            
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
            
            mod = [self checkError:result];
            
            
            if (error.code!=0) {
                mod.resltState = ResultNetError;
            }else{
                mod.resltState = ResultSuccess;
            }
            
        }else{
            
            mod.resltState = ResultNetError;
            mod.resultTip = @"没有网络";
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            callBack(mod);
        });
    });
}




//+(ResulteMod*)doPostWithOutCaCheWithUrl:(NSString *)urlString withFormdata:(NSDictionary *)formdata{
//    
//    
//    ResulteMod *mod = [ResulteMod new];
//    
//    if ([CheckNetWorkTool isExistenceNetwork])
//    {
//        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
//        
//        
//       urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//        NSURL *url=[NSURL URLWithString:urlString];
//        
//        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
//        
//        NSString *boundary = @"12436041281943726692693274280";
//        //设置请求体中内容
//        NSMutableString *bodyString = [[NSMutableString alloc]init];
//        int count = (int)([[formdata allKeys] count]-1);
//        for (int i=count; i>=0; i--) {
//            
//            NSString *key = [formdata allKeys][i];
//            NSString *value = [formdata allValues][i];
//            
//            [bodyString appendFormat:@"-----------------------------%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n",boundary,key,value];
//        }
//        
//        NSLog(@"post请求的url %@",url);
//        [bodyString appendFormat:@"-----------------------------%@--\r\n", boundary];
//        
//        NSLog(@"post请求的bodyData is %@",bodyString);
//        NSMutableData *bodyData = [[NSMutableData alloc]initWithLength:0];
//        NSData *bodyStringData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
//        [bodyData appendData:bodyStringData];
//        
//        NSString *contentLength = [NSString stringWithFormat:@"%ld",(long)[bodyData length]];
//        
//        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=---------------------------%@", boundary];
//        
//        [request setValue:contentType forHTTPHeaderField:@"Content-Type"];
//        [request setValue:contentLength forHTTPHeaderField:@"Content-Length"];
//        [request setHTTPBody:bodyData];
//        [request setHTTPMethod:@"POST"];
//        
//        
//        
//        NSLog(@"timeout: %f", request.timeoutInterval);   // Displays 240.000000
//        
//        //定义
//        NSHTTPURLResponse* urlResponse = nil;
//        NSError *error = [[NSError alloc] init];
//        
//        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
//        
//        NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//        result = [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//去前后空格
//        
//        
//        NSLog(@"DoPost====== 获取数据%@",result);
//        
//        
//        
//        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
//        
//        mod = [self checkError:result];
//        
//        
//        if (error.code!=0) {
//            mod.resltState = ResultNetError;
//        }else{
//            mod.resltState = ResultSuccess;
//        }
//        
//    }else{
//        
//        mod.resltState = ResultNetError;
//        mod.resultTip = @"没有网络";
//    }
//    
//    return mod;
//    
//}

+(ResulteMod*)doPostWithOutCaCheWithUrl:(NSString *)urlString withFormdata:(NSDictionary *)formdata{
    
    
    ResulteMod *mod = [ResulteMod new];
    
    if ([CheckNetWorkTool isExistenceNetwork])
    {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
        
        
        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url=[NSURL URLWithString:urlString];
        
        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
        
        //        NSString *boundary = @"12436041281943726692693274280";
        NSString *boundary = @"12436041281943";
        
        //设置请求体中内容
        NSMutableString *bodyString = [[NSMutableString alloc]init];
        int count = (int)([[formdata allKeys] count]-1);
        for (int i=count; i>=0; i--) {
            
            NSString *key = [formdata allKeys][i];
            NSString *value = [formdata allValues][i];
            
            [bodyString appendFormat:@"-----------------------------%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n",boundary,key,value];
        }
        
        NSLog(@"post请求的url %@",url);
        [bodyString appendFormat:@"-----------------------------%@--\r\n", boundary];
        
        NSLog(@"post请求的bodyData is %@",bodyString);
        NSMutableData *bodyData = [[NSMutableData alloc]initWithLength:0];
        NSData *bodyStringData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
        [bodyData appendData:bodyStringData];
        
        NSString *contentLength = [NSString stringWithFormat:@"%ld",(long)[bodyData length]];
        
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=---------------------------%@", boundary];
        
        [request setValue:contentType forHTTPHeaderField:@"Content-Type"];
        [request setValue:@"2" forHTTPHeaderField:App_version_head_key];
        [request setValue:contentLength forHTTPHeaderField:@"Content-Length"];
        [request setHTTPBody:bodyData];
        [request setHTTPMethod:@"POST"];
        
        
        
        NSLog(@"timeout: %f", request.timeoutInterval);   // Displays 240.000000
        
        //定义
        NSHTTPURLResponse* urlResponse = nil;
        NSError *error = [[NSError alloc] init];
        
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
        
        NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        result = [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//去前后空格
        
        
        NSLog(@"DoPost====== 获取数据%@",result);
        
        
        
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        
        mod = [self checkError:result];
        
        
        if (error.code!=0) {
            mod.resltState = ResultNetError;
        }else{
            mod.resltState = ResultSuccess;
        }
        
    }else{
        
        mod.resltState = ResultNetError;
        mod.resultTip = @"没有网络";
    }
    
    return mod;
    
}


+(ResulteMod*)doPostWithOutCaChe:(NSString *)urlString withData:(NSString*)badyString{
    
    badyString = [badyString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    ResulteMod *mod = [ResulteMod new];
    if ([CheckNetWorkTool isExistenceNetwork])
    {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
        
        NSString *urlAll = [urlString stringByAppendingString:badyString];
        NSLog(@"doPost 访问%@",urlAll);
        
        NSURL *url=[NSURL URLWithString:urlString];
        
        NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
        [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [urlRequest setValue:@"2" forHTTPHeaderField:App_version_head_key];
        [urlRequest setTimeoutInterval:8.0f];//设置超时秒
        [urlRequest setHTTPMethod:@"POST"];
        [urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
        
        
        [urlRequest setHTTPBody:[badyString dataUsingEncoding:NSUTF8StringEncoding]];//???
        
        NSLog(@"timeout: %f", urlRequest.timeoutInterval);   // Displays 240.000000
        
        //定义
        NSHTTPURLResponse* urlResponse = nil;
        NSError *error = [[NSError alloc] init];
        
        NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&error];
        
        NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        result = [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//去前后空格
        
        NSLog(@"DoPost 获取数据%@",result);
        
        NSLog(@"请求数据完毕");
        
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        mod = [self checkError:result];
        
        if (error.code!=0) {
            mod.resltState = ResultNetError;
        }else{
            mod.resltState = ResultSuccess;
        }
        
    }else{
        mod.resltState = ResultNetError;
    }
    return mod;
}



//发送POST请求
+(void)doPostWithOutCaChe:(NSString *)urlString withData:(NSString*)badyString withCallBack:(FinishCallBack)block;
{
    block ([Connect doPostWithOutCaChe:urlString withData:badyString]);
}





+(void)doPostWithCache:(NSString *)urlString withData:(NSString*)badyString withCallBack:(FinishCallBack)block;
{
    block([Connect doPostWithCache:urlString withData:badyString]);
}


+(NSString *)doGetGbk:(NSString *)urlString
{
    if ([CheckNetWorkTool isExistenceNetwork]) {
        
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
        
        NSLog(@"doGet 访问%@",urlString);
        NSURLCache *urlCache = [NSURLCache sharedURLCache];
        /* 设置缓存的大小为1M*/
        [urlCache setMemoryCapacity:1*1024*1024];
        NSURL *url=[NSURL URLWithString:urlString];
        
        //创建一个请求
        NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:8.0f];
        [urlRequest setHTTPMethod:@"GET"];
        [urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
        //定义
        NSHTTPURLResponse* urlResponse = nil;
        NSError *error = [[NSError alloc] init];
        
        NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&error];
        
        NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
        
        NSString *result = [[NSString alloc] initWithData:responseData encoding:enc];
        result = [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//去前后空格
        
        NSLog(@"DoGet 获取数据%@",result);
        
        NSLog(@"请求数据完毕");
        if ([responseData length]>0) {
            NSString *key = [urlString MD5Hash];
            [FTWCache setObject:responseData forKey:key];//写入缓存
            NSLog(@"写入缓存完毕");
        }
        
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        
        if (error.code!=0) {
            return nil;
        }else{
            return result;
        }
        
    }else{
        return nil;
    }
}


+(NSString *)doGetGbkWithCaChe:(NSString *)urlString
{
    NSString *key = [urlString MD5Hash];
    NSData *resData = [FTWCache objectForKey:key];//从缓存中获取数据
    NSString *result=@"";
    
    if ([resData length]>0) {//有数据
        
        result = [[NSString alloc] initWithData:resData encoding:NSUTF8StringEncoding];
        NSLog(@"doGetWithCaChe 访问%@",urlString);
        NSLog(@"doGetWithCaChe 从缓存得到数据%@",result);
        
    } else {
        NSLog(@"缓存没有数据，请求数据");
        result =[self doGetGbk:urlString];
    }
    [self checkError:result];
    return result;
}



+(ResulteMod*)checkError:(NSString*)resString{
    
    ResulteMod *mod = [[ResulteMod alloc]init];
    
    if (resString==nil) {
        
        mod.resltState = ResultNetError;
        mod.resultCode = @"";
        
    }else if([StringTool isBlankString:resString]){// “”
        
        mod.resltState = ResultFail;
        mod.resultCode = @"返回空字符串";
        mod.resultTip = @"返回空字符串";
        
    }else{
        
        NSDictionary *resDic = [resString objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
        
        
        NSLog(@"=========%@",resDic);
        NSDictionary *abstractResponseDic = resDic[@"abstractResponse"];
        
        if (abstractResponseDic!=nil) {
            mod.resultCode = [NSString stringWithFormat:@"%@",[abstractResponseDic objectForKey:@"code"]];
            mod.resultTip = [StringTool isBlankString:abstractResponseDic[@"message"]]?@"":abstractResponseDic[@"message"];
        }else{
            mod.resultCode = [NSString stringWithFormat:@"%@",[resDic objectForKey:@"code"]];
            mod.resultTip = [StringTool isBlankString:resDic[@"message"]]?@"":resDic[@"message"];
        }
        
        
        mod.resultData = resString;
        mod.resultDic = resDic;
        mod.resltState = ResultSuccess;
    }
    
    return mod;
}





@end
